<?php
class Mail_Model_Mail extends Promotor_Model_Scaffold {
	protected $_dbTableClass = 'Mail_Model_DbTable_Mail';
	
	public function findEmailAddress($subject) {
		$matches = array();
		$pattern = '/([\wd_\-\.]+@[\wd_\-\.]+\.\w{2,3})/i';

		@preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER);

		
		if (count($matches)) {
			return $matches[1];
		}

		return $matches;
	}

	/**
	 * @param $subject
	 * @return unknown_type
	 */
	public function addMultiMails(array $emails) {
		$table = $this->getDbTable();
		$validator = new Zend_Validate_EmailAddress();

		foreach ($emails as $email) {
			if ($validator->isValid($email)) {
				$data = array('email' => $email);

				try {
					$table->insert($data);
					$this->_setStatus(self::SUCCESS);
				} catch(Zend_Exception $e) {
					$this->_addException($e);
					$this->_setStatus(self::FAILURE);
				}
			}
		}
	}
}